package cn.kpt.biz.service.impl;

import cn.kpt.beans.pojo.ItripImage;
import cn.kpt.beans.vo.ItripImageVO;
import cn.kpt.beans.vo.comment.ItripListCommentVO;
import cn.kpt.beans.vo.comment.ItripScoreCommentVO;
import cn.kpt.beans.vo.comment.ItripSearchCommentVO;
import cn.kpt.biz.service.CommentService;
import cn.kpt.dao.itripcomment.ItripCommentMapper;
import cn.kpt.dao.itripimage.ItripImageMapper;
import cn.kpt.utils.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @ClassName CommentServiceImpl
 * @Description TODO
 * @Author kangpengtao
 * @Date 2019/10/21 15:33
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private ItripCommentMapper itripCommentMapper;
    @Resource
    private ItripImageMapper itripImageMapper;

    @Override
    public ItripScoreCommentVO getCommentScoreByHotelId(Long hotelId) throws Exception {
        return itripCommentMapper.getItripCommentScore(hotelId);
    }

    @Override
    public Integer getItripCommentCountByMap(Map<String, Object> param) throws Exception {
        return itripCommentMapper.getItripCommentCountByMap(param);
    }

    @Override
    public void getCommentListForPaging(Page<ItripListCommentVO> page, ItripSearchCommentVO vo) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("hotelId", vo.getHotelId());
        map.put("isHavingImg", vo.getIsHavingImg());
        map.put("isOk", vo.getIsOk());
        Integer total = itripCommentMapper.getItripCommentCount(map);
        page.setTotal(total);

        map.put("startIndex", page.getBeginPos());
        map.put("pageSize", page.getPageSize());
        List<ItripListCommentVO> rows = itripCommentMapper.getItripCommentList(map);
        page.setRows(rows);
    }

    @Override
    public List<ItripImageVO> getCommentImageById(Long targetId) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("targetId", targetId);
        params.put("type", 2); // 评论图片
        List<ItripImage> itripImages = itripImageMapper.getItripImageListByMap(params);

        // 转换
        List<ItripImageVO> vos = new ArrayList<>();
        for (ItripImage itripImage : itripImages) {
            ItripImageVO vo = new ItripImageVO();
            vo.setImgUrl(itripImage.getImgUrl());
            vo.setPosition(itripImage.getPosition());
            vos.add(vo);
        }
        return vos;

    }
}
